{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Advanced Tutorial"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Adding visualization\n",
    "\n",
    "So far, we've built a model, run it, and analyzed some output afterwards. However, one of the advantages of agent-based models is that we can often watch them run step by step, potentially spotting unexpected patterns, behaviors or bugs, or developing new intuitions, hypotheses, or insights. Other times, watching a model run can explain it to an unfamiliar audience better than static explanations. Like many ABM frameworks, Mesa allows you to create an interactive visualization of the model. In this section we'll walk through creating a visualization using built-in components, and (for advanced users) how to create a new visualization element.\n",
    "\n",
    "First, a quick explanation of how Mesa's interactive visualization works. Visualization is done in a browser window, using JavaScript to draw the different things being visualized at each step of the model. To do this, Mesa launches a small web server, which runs the model, turns each step into a JSON object (essentially, structured plain text) and sends those steps to the browser.\n",
    "\n",
    "A visualization is built up of a few different modules: for example, a module for drawing agents on a grid, and another one for drawing a chart of some variable. Each module has a Python part, which runs on the server and turns a model state into JSON data; and a JavaScript side, which takes that JSON data and draws it in the browser window. Mesa comes with a few modules built in, and let you add your own as well."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Grid Visualization\n",
    "\n",
    "To start with, let's have a visualization where we can watch the agents moving around the grid. For this, you will need to put your model code in a separate Python source file; for example, `MoneyModel.py`. Next, either in the same file or in a new one (e.g. `MoneyModel_Viz.py`) import the server class and the Canvas Grid class (so-called because it uses HTML5 canvas to draw a grid). If you're in a new file, you'll also need to import the actual model object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from mesa.visualization.modules import CanvasGrid\n",
    "from mesa.visualization.ModularVisualization import ModularServer\n",
    "\n",
    "# If MoneyModel.py is where your code is:\n",
    "# from MoneyModel import MoneyModel"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`CanvasGrid` works by looping over every cell in a grid, and generating a portrayal for every agent it finds. A portrayal is a dictionary (which can easily be turned into a JSON object) which tells the JavaScript side how to draw it. The only thing we need to provide is a function which takes an agent, and returns a portrayal object. Here's the simplest one: it'll draw each agent as a red, filled circle which fills half of each cell."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def agent_portrayal(agent):\n",
    "    portrayal = {\"Shape\": \"circle\",\n",
    "                 \"Color\": \"red\",\n",
    "                 \"Filled\": \"true\",\n",
    "                 \"Layer\": 0,\n",
    "                 \"r\": 0.5}\n",
    "    return portrayal"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In addition to the portrayal method, we instantiate a canvas grid with its width and height in cells, and in pixels. In this case, let's create a 10x10 grid, drawn in 500 x 500 pixels."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "grid = CanvasGrid(agent_portrayal, 10, 10, 500, 500)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we create and launch the actual server. We do this with the following arguments:\n",
    "\n",
    "* The model class we're running and visualizing; in this case, `MoneyModel`.\n",
    "* A list of module objects to include in the visualization; here, just `[grid]`\n",
    "* The title of the model: \"Money Model\"\n",
    "* Any inputs or arguments for the model itself. In this case, 100 agents, and height and width of 10.\n",
    "\n",
    "Once we create the server, we set the port for it to listen on (you can treat this as just a piece of the URL you'll open in the browser). Finally, when you're ready to run the visualization, use the server's `launch()` method.\n",
    "\n",
    "```python\n",
    "server = ModularServer(MoneyModel, \n",
    "                       [grid], \n",
    "                       \"Money Model\", \n",
    "                       100, 10, 10)\n",
    "server.port = 8521 # The default\n",
    "server.launch()\n",
    "```\n",
    "The full code should now look like:\n",
    "\n",
    "```python\n",
    "from MoneyModel import *\n",
    "from mesa.visualization.modules import CanvasGrid\n",
    "from mesa.visualization.ModularVisualization import ModularServer\n",
    "\n",
    "\n",
    "def agent_portrayal(agent):\n",
    "    portrayal = {\"Shape\": \"circle\",\n",
    "                 \"Filled\": \"true\",\n",
    "                 \"Layer\": 0,\n",
    "                 \"Color\": \"red\",\n",
    "                 \"r\": 0.5}\n",
    "    return portrayal\n",
    "\n",
    "grid = CanvasGrid(agent_portrayal, 10, 10, 500, 500)\n",
    "server = ModularServer(MoneyModel, \n",
    "                       [grid], \n",
    "                       \"Money Model\", \n",
    "                       100, 10, 10)\n",
    "server.port = 8521 # The default\n",
    "server.launch()\n",
    "```\n",
    "Now run this file; this should launch the interactive visualization server and open your web browser automatically. (If the browser doesn't open automatically, try pointing it at [http://127.0.0.1:8521](http://127.0.0.1:8521) manually. If this doesn't show you the visualization, something may have gone wrong with the server launch.)\n",
    "\n",
    "You should see something like the figure below: the model title, an empty space where the grid will be, and a control panel off to the right.\n",
    "\n",
    "![Empty Visualization](files/viz_empty.png)\n",
    "\n",
    "Click the 'reset' button on the control panel, and you should see the grid fill up with red circles, representing agents.\n",
    "\n",
    "![Redcircles Visualization](files/viz_redcircles.png)\n",
    "\n",
    "Click 'step' to advance the model by one step, and the agents will move around. Click 'run' and the agents will keep moving around, at the rate set by the 'fps' (frames per second) slider at the top. Try moving it around and see how the speed of the model changes. Pressing 'pause' will (as you'd expect) pause the model; presing 'run' again will restart it. Finally, 'reset' will start a new instantiation of the model.\n",
    "\n",
    "To stop the visualization server, go back to the terminal where you launched it, and press Control+c."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "#### Changing the agents\n",
    "\n",
    "In the visualization above, all we could see is the agents moving around -- but not how much money they had, or anything else of interest. Let's change it so that agents who are broke (wealth 0) are drawn in grey, smaller, and above agents who still have money.\n",
    "\n",
    "To do this, we go back to our `agent_portrayal` code and add some code to change the portrayal based on the agent properties.\n",
    "\n",
    "```python\n",
    "def agent_portrayal(agent):\n",
    "    portrayal = {\"Shape\": \"circle\",\n",
    "                 \"Filled\": \"true\",\n",
    "                 \"r\": 0.5}\n",
    "\n",
    "    if agent.wealth > 0:\n",
    "        portrayal[\"Color\"] = \"red\"\n",
    "        portrayal[\"Layer\"] = 0\n",
    "    else:\n",
    "        portrayal[\"Color\"] = \"grey\"\n",
    "        portrayal[\"Layer\"] = 1\n",
    "        portrayal[\"r\"] = 0.2\n",
    "    return portrayal\n",
    "```\n",
    "\n",
    "Now launch the server again - this will open a new browser window pointed at the updated visualization. Initially it looks the same, but advance the model and smaller grey circles start to appear. Note that since the zero-wealth agents have a higher layer number, they are drawn on top of the red agents.\n",
    "\n",
    "![Greycircles Visualization](files/viz_greycircles.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Adding a chart\n",
    "\n",
    "Next, let's add another element to the visualization: a chart, tracking the model's Gini Coefficient. This is another built-in element that Mesa provides.\n",
    "\n",
    "```python\n",
    "from mesa.visualization.modules import ChartModule\n",
    "```\n",
    "\n",
    "The basic chart pulls data from the model's DataCollector, and draws it as a line graph using the [Charts.js](http://www.chartjs.org/) JavaScript libraries. We instantiate a chart element with a list of series for the chart to track. Each series is defined in a dictionary, and has a `Label` (which must match the name of a model-level variable collected by the DataCollector) and a `Color` name. We can also give the chart the name of the DataCollector object in the model.\n",
    "\n",
    "Finally, we add the chart to the list of elements in the server. The elements are added to the visualization in the order they appear, so the chart will appear underneath the grid.\n",
    "\n",
    "```python\n",
    "chart = ChartModule([{\"Label\": \"Gini\", \n",
    "                      \"Color\": \"Black\"}],\n",
    "                    data_collector_name='datacollector')\n",
    "\n",
    "server = ModularServer(MoneyModel, \n",
    "                       [grid, chart], \n",
    "                       \"Money Model\", \n",
    "                       100, 10, 10)\n",
    "```\n",
    "\n",
    "Launch the visualization and start a model run, and you'll see a line chart underneath the grid. Every step of the model, the line chart updates along with the grid. Reset the model, and the chart resets too.\n",
    "\n",
    "![Chart Visualization](files/viz_chart.png)\n",
    "\n",
    "**Note:** You might notice that the chart line only starts after a couple of steps; this is due to a bug in Charts.js which will hopefully be fixed soon."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Building your own visualization component\n",
    "\n",
    "**Note:** This section is for users who have a basic familiarity with JavaScript. If that's not you, don't worry! (If you're an advanced JavaScript coder and find things that we've done wrong or inefficiently, please [let us know](https://github.com/projectmesa/mesa/issues)!)\n",
    "\n",
    "If the visualization elements provided by Mesa aren't enough for you, you can build your own and plug them into the model server.\n",
    "\n",
    "First, you need to understand how the visualization works under the hood. Remember that each visualization module has two sides: a Python object that runs on the server and generates JSON data from the model state (the server side), and a JavaScript object that runs in the browser and turns the JSON into something it renders on the screen (the client side).\n",
    "\n",
    "Obviously, the two sides of each visualization must be designed in tandem. They result in one Python class, and one JavaScript `.js` file. The path to the JavaScript file is a property of the Python class.\n",
    "\n",
    "For this example, let's build a simple histogram visualization, which can count the number of agents with each value of wealth. We'll use the [Charts.js](http://www.chartjs.org/) JavaScript library, which is already included with Mesa. If you go and look at its documentation, you'll see that it had no histogram functionality, which means we have to build our own out of a bar chart. We'll keep the histogram as simple as possible, giving it a fixed number of integer bins. If you were designing a more general histogram to add to the Mesa repository for everyone to use across different models, obviously you'd want something more general."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Client-Side Code\n",
    "\n",
    "In general, the server- and client-side are written in tandem. However, if you're like me and more comfortable with Python than JavaScript, it makes sense to figure out how to get the JavaScript working first, and then write the Python to be compatible with that.\n",
    "\n",
    "In the same directory as your model, create a new file called `HistogramModule.js`. This will store the JavaScript code for the client side of the new module.\n",
    "\n",
    "JavaScript classes can look alien to people coming from other languages -- specifically, they can look like functions. (The Mozilla [Introduction to Object-Oriented JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript) is a good starting point). In `HistogramModule.js`, start by creating the class itself:\n",
    "\n",
    "```javascript\n",
    "var HistogramModule = function(bins, canvas_width, canvas_height) {\n",
    "    // The actual code will go here.\n",
    "};\n",
    "```\n",
    "\n",
    "Note that our object is instantiated with three arguments: the number of integer bins, and the width and height (in pixels) the chart will take up in the visualization window.\n",
    "\n",
    "When the visualization object is instantiated, the first thing it needs to do is prepare to draw on the current page. To do so, it adds a [canvas](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API) tag to the page, using [JQuery's](https://jquery.com/) dollar-sign syntax (JQuery is already included with Mesa). It also gets the canvas' context, which is required for doing anything with it.\n",
    "\n",
    "```javascript\n",
    "var HistogramModule = function(bins, canvas_width, canvas_height) {\n",
    "    // Create the tag:\n",
    "    var canvas_tag = \"<canvas width='\" + canvas_width + \"' height='\" + canvas_height + \"' \";\n",
    "    canvas_tag += \"style='border:1px dotted'></canvas>\";\n",
    "    // Append it to #elements:\n",
    "    var canvas = $(canvas_tag)[0];\n",
    "    $(\"#elements\").append(canvas);\n",
    "    // Create the context and the drawing controller:\n",
    "    var context = canvas.getContext(\"2d\");\n",
    "};\n",
    "```\n",
    "\n",
    "Look at the Charts.js [bar chart documentation](http://www.chartjs.org/docs/#bar-chart-introduction).  You'll see some of the boilerplate needed to get a chart set up. Especially important is the `data` object, which includes the datasets, labels, and color options. In this case, we want just one dataset (we'll keep things simple and name it \"Data\"); it has `bins` for categories, and the value of each category starts out at zero. Finally, using these boilerplate objects and the canvas context we created, we can create the chart object.\n",
    "\n",
    "```javascript\n",
    "var HistogramModule = function(bins, canvas_width, canvas_height) {\n",
    "    // Create the tag:\n",
    "    var canvas_tag = \"<canvas width='\" + canvas_width + \"' height='\" + canvas_height + \"' \";\n",
    "    canvas_tag += \"style='border:1px dotted'></canvas>\";\n",
    "    // Append it to #elements:\n",
    "    var canvas = $(canvas_tag)[0];\n",
    "    $(\"#elements\").append(canvas);\n",
    "    // Create the context and the drawing controller:\n",
    "    var context = canvas.getContext(\"2d\");\n",
    "\n",
    "    // Prep the chart properties and series:\n",
    "    var datasets = [{\n",
    "        label: \"Data\",\n",
    "        fillColor: \"rgba(151,187,205,0.5)\",\n",
    "        strokeColor: \"rgba(151,187,205,0.8)\",\n",
    "        highlightFill: \"rgba(151,187,205,0.75)\",\n",
    "        highlightStroke: \"rgba(151,187,205,1)\",\n",
    "        data: []\n",
    "    }];\n",
    "\n",
    "    // Add a zero value for each bin\n",
    "    for (var i in bins)\n",
    "        datasets[0].data.push(0);\n",
    "\n",
    "    var data = {\n",
    "        labels: bins,\n",
    "        datasets: datasets\n",
    "    };\n",
    "\n",
    "    var options = {\n",
    "        scaleBeginsAtZero: true\n",
    "    };\n",
    "\n",
    "    // Create the chart object\n",
    "    var chart = new Chart(context, {type: 'bar', data: data, options: options});\n",
    "\n",
    "    // Now what?\n",
    "};\n",
    "```\n",
    "\n",
    "There are two methods every client-side visualization class must implement to be able to work: `render(data)` to render the incoming data, and `reset()` which is called to clear the visualization when the user hits the reset button and starts a new model run.\n",
    "\n",
    "In this case, the easiest way to pass data to the histogram is as an array, one value for each bin. We can then just loop over the array and update the values in the chart's dataset.\n",
    "\n",
    "There are a few ways to reset the chart, but the easiest is probably to destroy it and create a new chart object in its place.\n",
    "\n",
    "With that in mind, we can add these two methods to the class:\n",
    "\n",
    "```javascript\n",
    "var HistogramModule = function(bins, canvas_width, canvas_height) {\n",
    "    // ...Everything from above...\n",
    "    this.render = function(data) {\n",
    "        datasets[0].data = data;\n",
    "        chart.update();\n",
    "    };\n",
    "\n",
    "    this.reset = function() {\n",
    "        chart.destroy();\n",
    "        chart = new Chart(context, {type: 'bar', data: data, options: options});\n",
    "    };\n",
    "};\n",
    "```\n",
    "\n",
    "Note the `this`. before the method names. This makes them public and ensures that they are accessible outside of the object itself. All the other variables inside the class are only accessible inside the object itself, but not outside of it."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Server-Side Code\n",
    "\n",
    "Can we get back to Python code? Please?\n",
    "\n",
    "Every JavaScript visualization element has an equal and opposite server-side Python element. The Python class needs to also have a `render` method, to get data out of the model object and into a JSON-ready format. It also needs to point towards the code where the relevant JavaScript lives, and add the JavaScript object to the model page.\n",
    "\n",
    "In a Python file (either its own, or in the same file as your visualization code), import the `VisualizationElement` class we'll inherit from, and create the new visualization class.\n",
    "\n",
    "```python\n",
    "    from mesa.visualization.ModularVisualization import VisualizationElement\n",
    "\n",
    "    class HistogramModule(VisualizationElement):\n",
    "        package_includes = [\"Chart.min.js\"]\n",
    "        local_includes = [\"HistogramModule.js\"]\n",
    "\n",
    "        def __init__(self, bins, canvas_height, canvas_width):\n",
    "            self.canvas_height = canvas_height\n",
    "            self.canvas_width = canvas_width\n",
    "            self.bins = bins\n",
    "            new_element = \"new HistogramModule({}, {}, {})\"\n",
    "            new_element = new_element.format(bins, \n",
    "                                             canvas_width, \n",
    "                                             canvas_height)\n",
    "            self.js_code = \"elements.push(\" + new_element + \");\"\n",
    "```\n",
    "\n",
    "There are a few things going on here. `package_includes` is a list of JavaScript files that are part of Mesa itself that the visualization element relies on. You can see the included files in [mesa/visualization/templates/](https://github.com/projectmesa/mesa/tree/main/mesa/visualization/templates). Similarly, `local_includes` is a list of JavaScript files in the same directory as the class code itself. Note that both of these are class variables, not object variables -- they hold for all particular objects.\n",
    "\n",
    "Next, look at the `__init__` method. It takes three arguments: the number of bins, and the width and height for the histogram. It then uses these values to populate the `js_code` property; this is code that the server will insert into the visualization page, which will run when the page loads. In this case, it creates a new HistogramModule (the class we created in JavaScript in the step above) with the desired bins, width and height; it then appends  (`push`es) this object to `elements`, the list of visualization elements that the visualization page itself maintains.\n",
    "\n",
    "Now, the last thing we need is the `render` method. If we were making a general-purpose visualization module we'd want this to be more general, but in this case we can hard-code it to our model.\n",
    "\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "class HistogramModule(VisualizationElement):\n",
    "    # ... Everything from above...\n",
    "\n",
    "    def render(self, model):\n",
    "        wealth_vals = [agent.wealth for agent in model.schedule.agents]\n",
    "        hist = np.histogram(wealth_vals, bins=self.bins)[0]\n",
    "        return [int(x) for x in hist]\n",
    "```\n",
    "\n",
    "Every time the render method is called (with a model object as the argument) it uses numpy to generate counts of agents with each wealth value in the bins, and then returns a list of these values. Note that the `render` method doesn't return a JSON string -- just an object that can be turned into JSON, in this case a Python list (with Python integers as the values; the `json` library doesn't like dealing with numpy's integer type).\n",
    "\n",
    "Now, you can create your new HistogramModule and add it to the server:\n",
    "\n",
    "```python\n",
    "    histogram = HistogramModule(list(range(10)), 200, 500)\n",
    "    server = ModularServer(MoneyModel, \n",
    "                           [grid, histogram, chart], \n",
    "                           \"Money Model\", \n",
    "                           100, 10, 10)\n",
    "    server.launch()\n",
    "```\n",
    "\n",
    "Run this code, and you should see your brand-new histogram added to the visualization and updating along with the model!\n",
    "\n",
    "![Histogram Visualization](files/viz_histogram.png)\n",
    "\n",
    "If you've felt comfortable with this section, it might be instructive to read the code for the [ModularServer](https://github.com/projectmesa/mesa/blob/main/mesa/visualization/ModularVisualization.py#L259) and the [modular_template](https://github.com/projectmesa/mesa/blob/main/mesa/visualization/templates/modular_template.html) to get a better idea of how all the pieces fit together."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Happy Modeling!\n",
    "\n",
    "This document is a work in progress.  If you see any errors, exclusions or have any problems please contact [us](https://github.com/projectmesa/mesa/issues)."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
